'use strict';

const {
	Controller
} = require('uni-cloud-router');
const {
	httpClientCode
} = require("config");
const {
	getUserInfo
} = require("user-info-admin");
const db = uniCloud.database();
const dbCmd = db.command;
module.exports = class CouponController extends Controller {
	async getMobile(event) {
		let response = {
			code: httpClientCode.ERROR.value,
			msg: '查询失败',
			data: null
		};
		let id = event.data.id
		let uniIdToken = event.event.uniIdToken
		let userInfo = await getUserInfo(uniIdToken)
		let appletInfoData = await db.collection('wmz-applet-info').doc(userInfo.app_id).get()
		appletInfoData = appletInfoData.data[0]
		if (appletInfoData && appletInfoData.balance) {
			let money = 30 * 100
			if (appletInfoData.balance < money) {
				response.code = httpClientCode.ERROR.value;
				response.msg = '余额不足';
				response.data = 0
			} else {
				let balance = appletInfoData.balance
				balance = balance - money
				await db.collection('wmz-applet-info').doc(userInfo.app_id).update({
					balance: balance
				})
				let consult = await db.collection('wmzmall-app-consult').doc(id).get()
				consult = consult.data[0]
				await db.collection('wmzmall-app-consult').doc(id).update({
					qmobile: consult.mobile,
					query: true,
					query_time: new Date().getTime()
				})
				response.code = httpClientCode.SUCCESS.value;
				response.msg = '成功';
				response.data = consult.mobile
			}

		} else {
			response.code = httpClientCode.ERROR.value;
			response.msg = '余额不足';
			response.data = 0
		}
		return response;
	}
}